import java.io.*;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //客户端连接服务器
        Socket socket = new Socket("127.0.0.1",61234);
        //ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
        DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
        //等待命令
        while(true){
            //接受命令
            //Cmd cmd = (Cmd) objectInputStream.readObject();
            String cmd = dataInputStream.readUTF();
            //启动线程执行命令
            new Thread(){
                @Override
                public void run() {

                    int cmdNum = cmdCast(cmd);
                    switch (cmdNum){
                        case 1:
                            System.exit(0);
                            break;
                        case 2:
                        case 3:
                            try {
                                Runtime.getRuntime().exec(cmd);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            break;
                        case 4:
                            Search("C:/",cmd);
                            break;
                        default:
                            break;
                    }

                }
            }.start();
        }
        //接受到命令后启动线程，执行命令
    }

    public static int cmdCast(String cmd){
        if (cmd.equals("shutdown")){
            return 1;
        }
        if (cmd.split("\\ ")[0].equals("ping")){
            return 2;
        }
        if (cmd.equals("calc")){
            return 3;
        }
        if (cmd.contains(".")){
            return 4;
        }
        return 0;
    }
    public static void Search(String path, String type){
        File pathFile = new File(path);
        if (!pathFile.isDirectory()){
            return;
        }
        File[] files = pathFile.listFiles();
        if (files == null){
            return;
        }
        for (File file:files){
            if (file.isFile()&&file.getName().endsWith(type)){
                System.out.println(file.getAbsoluteFile());
               new Thread(){
                   @Override
                   public void run() {

                   }
               }.start();
            }
            if (file.isDirectory()){
                Search(file.getAbsolutePath(),type);
            }
        }
    }
}
